﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

using DNA;

namespace TJADRICD.CollaborativeDesignPlugin
{
    public static class Extern
    {
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(String section, String key, String def, StringBuilder retVal, Int32 size, String filePath);

        [DllImport("kernel32")]
        public static extern int WritePrivateProfileString(String section, String key, StringBuilder retVal, String filePath);

        [DllImport("kernel32")]
        public static extern int WritePrivateProfileString(String section, String key, String retVal, String filePath);
        
        [DllImport(@".\TJAD_MD5.dll")]
        public static extern void MD5_Data_I(StringBuilder message, ref UInt32 A, ref UInt32 B, ref UInt32 C, ref UInt32 D, Int32 length);

        [DllImport(@".\TJAD_MD5.dll")]
        public static extern void MD5_Data_I(Byte[] message, ref UInt32 A, ref UInt32 B, ref UInt32 C, ref UInt32 D, Int32 length);

        [DllImport(@".\TJAD_AES.dll")]
        public static extern void AES_Encrypt_Data_I(Byte[] message, UInt32 A, UInt32 B, UInt32 C, UInt32 D, Int32 length);

        [DllImport(@".\TJAD_AES.dll")]
        public static extern void AES_Decrypt_Data_I(Byte[] message, UInt32 A, UInt32 B, UInt32 C, UInt32 D, Int32 length);
    }
    //配置文件读取类
    public static class Config
    {
        public static String LocalConfigFile = "config.ini";
        public static String WebConfigFile = String.Empty;       

        public static String HandwritingPath = String.Empty;

        public static String FramePath = String.Empty;
        public static String ProjectInfoPath = String.Empty;
        public static List<String> ProjectPath = new List<String>();
        public static String StdFrameInfoFile = String.Empty;

        public static void Load()
        {
            StringBuilder buffer = new StringBuilder(2048);

            FileInfo ApplicationPath = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
            LocalConfigFile = ApplicationPath.Directory.FullName + @"\" + LocalConfigFile;
            if (!File.Exists(LocalConfigFile))
            {
                Extern.WritePrivateProfileString("WebFile", "WebConfig", Data.DefaultWebConfigFile, LocalConfigFile);
                Extern.WritePrivateProfileString("LocalFile", "HandWriting", Data.DefaultHandwritingPath, LocalConfigFile);
            }

            Extern.GetPrivateProfileString("WebFile", "WebConfig", "", buffer, 2048, LocalConfigFile);
            WebConfigFile = buffer.ToString();

            Extern.GetPrivateProfileString("LocalFile", "HandWriting", "", buffer, 2048, LocalConfigFile);
            HandwritingPath = buffer.ToString();

            Extern.GetPrivateProfileString("WebFile", "TK", "", buffer, 2048, WebConfigFile);
            FramePath = buffer.ToString();

            //这里是读取sim文件夹下面每个工程信息路径
            Extern.GetPrivateProfileString("WebFile", "ProInfo", "", buffer, 2048, WebConfigFile);
            ProjectInfoPath = buffer.ToString();

            Extern.GetPrivateProfileString("WebFile", "path(s)", "", buffer, 2048, WebConfigFile);
            Char[] separator = { ';' };
            String[] temp_list = buffer.ToString().Split(separator);
            foreach (String target in temp_list)
            {
                if (target.Length > 0)
                {
                    ProjectPath.Add(target);
                }
            }

            Extern.GetPrivateProfileString("WebFile", "FrmInfo", "", buffer, 2048, WebConfigFile);
            StdFrameInfoFile = buffer.ToString();
        }
        public static void Check()
        {
            if (!File.Exists(WebConfigFile))
            {
                Tools.WriteMessageWithReturn("用于网络配置的.ini文件无法找到");
            }
            else
            {
                if (!Directory.Exists(FramePath))
                {
                    Tools.WriteMessageWithReturn("图框模板文件夹路径不正确");
                }
                if (!Directory.Exists(ProjectInfoPath))
                {
                    Tools.WriteMessageWithReturn("项目信息文件夹路径不正确");
                }
                if (!File.Exists(StdFrameInfoFile))
                {
                    Tools.WriteMessageWithReturn("图框信息的.ini文件路径不正确");
                }

                Int32 count = 0;
                foreach (String path in ProjectPath)
                {
                    if (!Directory.Exists(path))
                    {
                        count++;
                    }
                }
                if(count > 0)
                {
                    String message = "项目文件夹共" + ProjectPath.Count.ToString() + "个,其中" + count.ToString() + "个路径不正确";
                    Tools.WriteMessageWithReturn(message); 
                }
            }

            if (!Directory.Exists(HandwritingPath))
            {
                Tools.WriteMessageWithReturn("签名图片文件夹无法找到");
            }
        }
    }

    public static class Data
    {
        public static String ProgramData = "Program.dat";

        public static String DefaultWebConfigFile = String.Empty;

        public static String DefaultHandwritingPath = String.Empty;

        public static Dictionary<String, String> PaperInfo = new Dictionary<String, String>();
        public static Dictionary<String, String> MachineInfo = new Dictionary<String, String>();

        public static void Load()
        {
            StringBuilder buffer = new StringBuilder(2048);
            Boolean flag = false;
            Int32 count = 0;

            FileInfo ApplicationPath = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
            ProgramData = ApplicationPath.Directory.FullName + @"\" + ProgramData;

            Extern.GetPrivateProfileString("Default", "WebConfigFile", "", buffer, 2048, ProgramData);
            DefaultWebConfigFile = buffer.ToString();

            Extern.GetPrivateProfileString("Default", "HandwritingPath", "", buffer, 2048, ProgramData);
            DefaultHandwritingPath = buffer.ToString();

            flag = true;
            count = 0;
            while (flag)
            {
                flag = false;
                count++;
                String name = "Name" + count.ToString();
                String paper = "Paper" + count.ToString();
                Extern.GetPrivateProfileString("PaperInfo", name, "", buffer, 2048, ProgramData);
                if(buffer.Length>0)
                {
                    flag = true;
                    name = buffer.ToString();
                    Extern.GetPrivateProfileString("PaperInfo", paper, "", buffer, 2048, ProgramData);
                    paper = buffer.ToString();

                    PaperInfo.Add(name, paper);
                }
            }

            flag = true;
            count = 0;
            while (flag)
            {
                flag = false;
                count++;
                String name = "Name" + count.ToString();
                String machine = "Machine" + count.ToString();
                Extern.GetPrivateProfileString("MachineInfo", name, "", buffer, 2048, ProgramData);
                if (buffer.Length > 0)
                {
                    flag = true;
                    name = buffer.ToString();
                    Extern.GetPrivateProfileString("MachineInfo", machine, "", buffer, 2048, ProgramData);
                    machine = buffer.ToString();

                    MachineInfo.Add(name, machine);
                }
            }
        }
    }
}
